#!/usr/bin/env python3

import os
import sys
import time
# import struct
# import ctypes
# from ctypes import *

# # 参考：https://blog.csdn.net/gongjianbo1992/article/details/118019271


# # 如果指定大小端，可使用基类BigEndianStructure或LittleEndianStructure
# class MyStruct(ctypes.Structure):
#     _pack_ = 1  # 指定为1字节对齐
#     _fields_ = [
#         ("a", ctypes.c_char),
#         ("b", ctypes.c_int),
#         ("c", ctypes.c_double)
#     ]
 
# # 打印字节大小，测试pack设置是否有效
# print("sizeof MyStruct:", ctypes.sizeof(MyStruct))


# stdout_buffer = sys.stdout.buffer

# # data = b'Hello, World!'
# # stdout_buffer.write(data)

# myint = 2022
# stdout_buffer.write(myint.to_bytes(4, 'little'))


# # # 写入一个字节数组
# # byte_array = bytearray([65, 66, 67])  # A, B, C
# # stdout_buffer.write(byte_array)


# # print("sizeof MyStruct:", ctypes.sizeof(ctypes.c_long))















pathXio="/exbin/ipc/zzXio"
pathpid="/tmp/fcitx-dbus-pid.txt"
pathpip="/tmp/fcitx-dbus-addr.txt"

if os.path.exists(pathpid):
    os.remove(pathpid)
if os.path.exists(pathpip):
    os.remove(pathpip)


with open(pathXio, 'w') as file: 
    file.write('start-fcitx-dbus\n')
time.sleep(2)

if(not os.path.isfile(pathpid)):
    exit(-1)
if(not os.path.isfile(pathpip)):
    exit(-1)


stdout_buffer = sys.stdout.buffer

myint=0
strpth=""
with open('/tmp/fcitx-dbus-addr.txt', 'r') as file: 
    strpth = file.readline().strip('\n')

strpid=0
with open('/tmp/fcitx-dbus-pid.txt', 'r') as file: 
    strpid = file.readline().strip('\n')

stdout_buffer.write(strpth.encode('utf-8'))

myint=0
stdout_buffer.write(myint.to_bytes(1, 'little'))

myint=int(strpid)
stdout_buffer.write(myint.to_bytes(4, 'little'))

myint=0
stdout_buffer.write(myint.to_bytes(8, 'little'))
